package com.tuanzi.loan.web.bean.system;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.system.*;
import com.tuanzi.loan.business.service.system.DataPermissionRelationService;
import com.tuanzi.loan.business.service.system.FunctionalPermissionRelationService;
import com.tuanzi.loan.business.service.system.RoleService;
import com.tuanzi.loan.business.service.system.SystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.loan.web.domain.PermissionDomain;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class PermissionRelationBean extends JSFInfoBean<FunctionalPermission> {

    private static final long serialVersionUID = 6059675680981337738L;
    @Autowired
    private DataPermissionRelationService dataPermissionRelationService;
    @Autowired
    private FunctionalPermissionRelationService functionalPermissionRelationService;
    @Autowired
    private SystemUserService userService;
    @Autowired
    private RoleService roleService;

    private PermissionDomain permissionDomain;
    private String relationId;
    private RelationType relationType;

    private TreeNode[] selectedFunctionalTreeNodes;
    private TreeNode[] selectedDataTreeNodes;

    private TreeNode functionalRoot;
    private TreeNode dataRoot;

    @PostConstruct
    private void init() {
        relationId = getParamFromRequest("id");
        relationType = RelationType.valueOf(getParamFromRequest("relationType"));
        checkArgument(StringUtils.isNotBlank(relationId));

        List<String> functionalPermissionRelationIds = functionalPermissionRelationService
                .findAllByRelationId(relationId).parallelStream()
                .map(FunctionalPermissionRelation::getPermissionId).collect(Collectors.toList());

        List<String> dataPermissionRelationIds = dataPermissionRelationService
                .findAllByRelationId(relationId).parallelStream()
                .map(DataPermissionRelation::getPermissionId).collect(Collectors.toList());

        permissionDomain = new PermissionDomain(functionalPermissionRelationIds,
                dataPermissionRelationIds);
        functionalRoot = permissionDomain.generateFunctionalPermissionTreeNode();
        dataRoot = permissionDomain.generateDataPermissionTreeNode();
    }

    public void onSave() {
        List<String> functionalPermissionIds = Arrays.asList(selectedFunctionalTreeNodes)
                .parallelStream().map(x -> (FunctionalPermission) x.getData())
                .filter(x -> x.getId() != null).map(FunctionalPermission::getId)
                .collect(Collectors.toList());
        functionalPermissionRelationService.batchRelate(functionalPermissionIds, relationType,
                relationId);

        List<String> dataPermissionIds = Arrays.asList(selectedDataTreeNodes).parallelStream()
                .map(x -> (DataPermission) x.getData()).filter(x -> x.getId() != null)
                .map(DataPermission::getId).collect(Collectors.toList());
        dataPermissionRelationService.batchRelate(dataPermissionIds, relationType, relationId);
        addSuccessfullyMessage();
    }
}